package com.huike.five;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GamePanel extends JPanel implements ActionListener {
    private static final int ROWS = 15;
    private static final int COLS = 15;
    private GameFrame mainFrame;
    private JMenuBar jmb;

    Pointer[][] pointers = new Pointer[ROWS][COLS];
    private String gameFlag = "start";

    public GamePanel(GameFrame mainFrame) {
        //此方法是设置控件是否透明的。true表示不透明，false表示透明
        this.setOpaque(false);
        //设置jpane;的布局管理器为空
        this.setLayout(null);
        //获取窗体对象
        this.mainFrame = mainFrame;
        //创建菜单
        this.createMenu();
        //初始化点对象
        this.createPointers();
        //添加鼠标监听
        this.createMouseListener();
    }



    //创建字体
    private Font createFont() {
        return new Font("宋体", Font.BOLD, 22);
    }

    //创建菜单
    private void createMenu() {
        jmb = new JMenuBar();
        Font tfont = createFont();
        JMenu jMenu1 = new JMenu("游戏");
        jMenu1.setFont(tfont);
        JMenu jMenu2 = new JMenu("帮助");
        jMenu2.setFont(tfont);

        JMenuItem jmi1 = new JMenuItem("新游戏");
        jmi1.setFont(tfont);
        JMenuItem jmi2 = new JMenuItem("退出");
        jmi2.setFont(tfont);

        jMenu1.add(jmi1);
        jMenu1.add(jmi2);

        JMenuItem jmi3 = new JMenuItem("操作帮助");
        jmi3.setFont(tfont);
        JMenuItem jmi4 = new JMenuItem("胜利条件");
        jmi4.setFont(tfont);

        jMenu2.add(jmi3);
        jMenu2.add(jmi4);

        jmb.add(jMenu1);
        jmb.add(jMenu2);

        mainFrame.setJMenuBar(jmb);

        jmi1.addActionListener(this);
        jmi2.addActionListener(this);
        jmi3.addActionListener(this);
        jmi4.addActionListener(this);

        jmi1.setActionCommand("restart");
        jmi2.setActionCommand("exit");
        jmi3.setActionCommand("help");
        jmi4.setActionCommand("win");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("菜单子项被点击");
        String command = e.getActionCommand();
        System.out.println("指令：" + command);
    }

    private void createPointers() {
        int x = 0;
        int y = 0;
        int start = 26;
        Pointer pointer;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                x = j * 40 + start;
                y = i * 40 + start;
                pointers[i][j] = new Pointer(i, j, x, y);;
            }
        }
    }

    //重写父类的绘制方法
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //绘制ROW*COL的棋盘:画线
        drawGrid(g);
        //绘制5个点
        draw5Point(g);
        //绘制移动鼠标点的图标
        drawPointer(g);
    }

    private void drawGrid(Graphics g) {
        /**
         * Params:
         * x1 – the first point's x coordinate.  起始点的x坐标
         * y1 – the first point's y coordinate.  起始点的y坐标
         * x2 – the second point's x coordinate. 终点的x坐标
         * y2 – the second point's y coordinate. 终点的x坐标
         */
        //画线
        /*g.drawLine(26, 26, 26, 200);//竖线
        g.setColor(new Color(255,0,0));
        g.drawLine(26, 26, 200, 26);//横线
        g.setColor(new Color(255,255,0));
        g.drawLine(26, 26, 200, 200);//斜线*/

        int start = 26;
        int x1 = 26;
        int x2 = 586;
        int y1 = 26;
        int y2 = 26;
        int dis = 40;
        //横线:什么坐标在改变:y坐标,x1坐标=26，x2坐标=586
        for (int i = 0; i < ROWS; i++) {
            y1 = i * dis + start;
            y2 = y1;
            g.drawLine(x1, y1, x2, y2);
        }
        //竖线:什么坐标在改变:x坐标
        y1 = 26;
        y2 = 586;
        for (int i = 0; i < COLS; i++) {
            x1 = i * dis + start;
            x2 = x1;
            g.drawLine(x1, y1, x2, y2);
        }
    }

    private void draw5Point(Graphics g) {
        int x = 142;
        int y = 142;
        g.fillArc(x, y, 8, 8, 0, 360);
        x = 462;
        g.fillArc(x, y, 8, 8, 0, 360);
        x = 142;
        y = 462;
        g.fillArc(x, y, 8, 8, 0, 360);
        x = 462;
        g.fillArc(x, y, 8, 8, 0, 360);
        x = 302;
        y = 302;
        g.fillArc(x, y, 8, 8, 0, 360);
    }

    private void createMouseListener() {
        //实例化一个匿名内部类：MouseAdapter(抽象类)
        MouseAdapter mouseAdapter = new MouseAdapter() {
            //鼠标移动事件
            @Override
            public void mouseMoved(MouseEvent e) {
                //游戏是否开始标记
                if (!"start".equals(gameFlag)) {
                    return;
                }
                //System.out.println("鼠标移动");
                int x = e.getX();
                int y = e.getY();
                Pointer pointer;
                for (int i = 0; i < ROWS; i++) {
                    for (int j = 0; j < COLS; j++) {
                        pointer = pointers[i][j];
                        if (pointer.isPoint(x, y)) {
                            pointer.setShow(true);
                        } else {
                            pointer.setShow(false);
                        }
                    }
                }
                //告诉JPanel进行重新绘制
                repaint();
            }
        };
        addMouseMotionListener(mouseAdapter);
        addMouseListener(mouseAdapter);
    }

    private void drawPointer(Graphics g) {
        Pointer pointer;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                pointer = pointers[i][j];
                if (pointer != null) {
                    pointer.draw(g);
                }
            }
        }
    }
}
